package com.liao.excel.strategy;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.liao.excel.Cat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class ScreenValueStrategy extends AbstractMergeStrategy {
    /**
     * 分组，每几行合并一次
     */
    private List<List<Integer>> exportFieldGroupCountList;

    /**
     * 合并的目标开始列索引
     */
    private Integer[] targetBeginColIndex;
    /**
     * 合并的目标结束列索引
     */
    private Integer[] targetEndColIndex;
    /**
     * 需要开始合并单元格的首行索引
     */
    private Integer firstRowIndex;

    public ScreenValueStrategy() {
    }

    /**
     * @param exportDataList      待合并目标行的值
     * @param targetBeginColIndex 合并的目标开始列索引
     * @param targetEndColIndex   合并的目标结束列索引
     * @param firstRowIndex       需要开始合并单元格的首行索引
     */
    public ScreenValueStrategy(List<?> exportDataList, Integer[] targetBeginColIndex, Integer[] targetEndColIndex, Integer firstRowIndex) {
        this.exportFieldGroupCountList = getGroupCountList(exportDataList, targetBeginColIndex);
        this.targetBeginColIndex = targetBeginColIndex;
        this.targetEndColIndex = targetEndColIndex;
        this.firstRowIndex = firstRowIndex;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        for (int j = 0; j < targetBeginColIndex.length; j++) {
            if (cell.getRowIndex() == this.firstRowIndex && cell.getColumnIndex() >= targetBeginColIndex[j] - 1 && cell.getColumnIndex() <= targetEndColIndex[j] - 1) {
                int rowCount = this.firstRowIndex;
                for (Integer count : exportFieldGroupCountList.get(j)) {
                    if (count == 1) {
                        rowCount += count;
                        continue;
                    }
                    // 合并单元格
                    CellRangeAddress cellRangeAddress;
                    for (int i = 0; i < targetEndColIndex[j] - targetBeginColIndex[j] + 1; i++) {
                        cellRangeAddress = new CellRangeAddress(rowCount - 1, rowCount + count - 2, targetBeginColIndex[j] - 1 + i, targetBeginColIndex[j] - 1 + i);
                        sheet.addMergedRegionUnsafe(cellRangeAddress);
                    }
                    rowCount += count;
                }
            }
        }
    }

    /**
     * 该方法将目标列根据值是否相同连续可合并，存储可合并的行数
     *
     * @param exportDataList
     * @return
     */
    private List<List<Integer>> getGroupCountList(List<?> exportDataList, Integer[] indexCell) {
        if (CollectionUtils.isEmpty(exportDataList)) {
            return new ArrayList<>();
        }
        List<List<Integer>> list = new ArrayList<>();
        for (int index = 0; index < indexCell.length; index++) {
            List<Integer> groupCountList = new ArrayList<>();
            int count = 1;
            for (int i = 1; i < exportDataList.size(); i++) {
                boolean equals = isEquals(exportDataList.get(i), exportDataList.get(i - 1), index);
                if (equals) {
                    count++;
                } else {
                    groupCountList.add(count);
                    count = 1;
                }
            }
            // 处理完最后一条后
            groupCountList.add(count);
            list.add(groupCountList);
        }
        return list;
    }

    // 判断对应得列是否合并
    public boolean isEquals(Object detail1, Object detail2, int index) {
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();
        int type = 0;
        if (detail1 instanceof Cat) {
            cat1 = (Cat) detail1;
            cat2 = (Cat) detail2;
            type = 1;
        }
        if (index == 0) {
            return type == 1 ? cat1.getName().equals(cat2.getName()) :
                    cat1.getAge().equals(cat2.getAge())
                    ;
        }
        return false;
    }


}